package com.manage.auth;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.common.util.Page;
import com.common.util.Tools;
import com.model.auth.Role;
import com.model.auth.SResources;
import com.opensymphony.xwork2.ActionSupport;
import com.service.auth.IResService;
import com.service.auth.IRoleResService;
import com.service.auth.IRoleService;

public class RoleAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(RoleAction.class);
	private Integer pageNo=1;
	private Integer pageSize =20;
	private List<Role> roleList;
	private Integer roleCount;
	private Role role;
	private Integer id;
	private String ids;
	private String result;
	private Page page;
	
	private String resIds;
	
	private List<String> resIdList;
	private List<SResources> resAllList;
	
	@Resource
	private IRoleService roleService;
	
	@Resource
	private IResService resService;
	
	@Resource
	private IRoleResService roleResService;
	
	
	public String roleRes(){
		try{
			role =  roleService.getRoleById(id);
			resIdList = roleResService.getAllResByRoleId(id);//查询到角色的所有资源
			resAllList = resService.getAllMenus();
		}catch(Exception e){
			
			log.error("资源授权模块列表异常", e);
		}
		return "roleRes";
	}
	
	/**
	 * 更新用户角色信息
	 * @return
	 */
	public String updateRoleRes(){
		try{
			if(id!=null){
				roleResService.delAllResByRoleId(id);
			}
			if(StringUtils.isNotBlank(ids) && id!=null){
				String[] idList =ids.split(",");
				for(int i=0;i<idList.length;i++){
					roleResService.insertRoleRes(id, Integer.valueOf(idList[i]));
				}
			}
			
			Tools.cleanResource();
			
			HttpServletResponse response = ServletActionContext.getResponse();    
	        //设置字符集    
	        response.setCharacterEncoding("UTF-8");    
	        PrintWriter out = response.getWriter();    
	        //直接输入响应的内容    
	        out.println(1);
	        out.flush();    
	        out.close();
		}catch(Exception e){
			log.error("删除模块列表异常", e);
			result="error";
		}
		return null;
	}
	
	
	public String list() {
		try {
			roleCount = roleService.getRoleConunt();
			page = new Page(roleCount,pageSize,pageNo);
			roleList =  roleService.getRolePageInfo(pageNo, pageSize);
		} catch (Exception e) {
			log.error("资源模块列表异常", e);
		}
		return "list";
	}
	
	
	public String add(){
		
		
		return "add";
	}
	
	
	public String save(){
		try{
			if(role!=null){
				role.setCreateDate(new Date());
				roleService.insertRole(role);
				Tools.cleanResource();
			}
			result = "1";
		}catch(Exception e){
			result = "0";
			log.error("保存模块列表异常", e);
		}
		return "add";
	}
	
	public String deleteRole(){
		try{
			if(id!=null){
				roleService.delRoleById(id);
			}
			if(StringUtils.isNotBlank(ids)){
				String[] idList =ids.split(",");
				for(int i=0;i<idList.length;i++){
					roleService.delRoleById(Integer.valueOf(idList[i]));
				}
			}
			Tools.cleanResource();
			
			HttpServletResponse response = ServletActionContext.getResponse();    
	        //设置字符集    
	        response.setCharacterEncoding("UTF-8");    
	        PrintWriter out = response.getWriter();    
	        //直接输入响应的内容    
	        out.println(1);
	        out.flush();    
	        out.close();
		}catch(Exception e){
			log.error("删除模块列表异常", e);
			result="error";
		}
		return null;
	}
	
	public String edit(){
		try{
			if(id!=null){
				role = roleService.getRoleById(id);
			}
		}catch(Exception e){
			log.error("查询模块详细信息", e);
		}
		return "edit";
	}
	
	/**
	 * 更新模块详细信息
	 * @return
	 */
	public String update(){
		try{
			if(role!=null){
				roleService.updateRole(role);
				Tools.cleanResource();
			}
			result = "1";
		}catch(Exception e){
			result = "0";
			log.error("更新模块详细信息", e);
		}
		return "edit";
	}
	
	
	public List<String> getResIdList() {
		return resIdList;
	}

	public void setResIdList(List<String> resIdList) {
		this.resIdList = resIdList;
	}

	public List<SResources> getResAllList() {
		return resAllList;
	}


	public void setResAllList(List<SResources> resAllList) {
		this.resAllList = resAllList;
	}


	public String getResIds() {
		return resIds;
	}

	public void setResIds(String resIds) {
		this.resIds = resIds;
	}

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public Integer getRoleCount() {
		return roleCount;
	}

	public void setRoleCount(Integer roleCount) {
		this.roleCount = roleCount;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public IRoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	
}
